package com.ruoyi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.app.domain.DebtRecords;
import com.ruoyi.app.domain.ShopInfo;
import com.ruoyi.app.domain.ShopProfession;
import com.ruoyi.app.domain.ShopUser;
import com.ruoyi.app.domain.vo.*;
import com.ruoyi.app.mapper.ShopInfoMapper;
import com.ruoyi.app.mapper.SysUserShopMapper;
import com.ruoyi.app.service.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.MybatisUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-21
 */
@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> implements IShopInfoService
{
    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Autowired
    private IShopProfessionService shopProfessionService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    IShopUserService shopUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    IOrderInfoService orderInfoService;


    @Autowired
    IRestockInfoService restockInfoService;


    @Autowired
    IDebtRecordsService debtRecordsService;

    /**
     * 查询店铺信息
     * 
     * @param id 店铺信息主键
     * @return 店铺信息
     */
    @Override
    public ShopInfo selectShopInfoById(String id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询店铺信息列表
     * 
     * @param shopInfo 店铺信息
     * @return 店铺信息
     */
    @Override
    public List<ShopInfo> selectShopInfoList(ShopInfo shopInfo)
    {
        return MybatisUtils.list(baseMapper, shopInfo);
    }

    @Transactional
    @Override
    public Boolean createShopInfo(ShopInfoVo shopInfoVo) {
        BeanValidators.isNotEmpty(shopInfoVo.getShopName());
        // 添加店铺
        ShopInfo shopInfo = new ShopInfo();
        BeanUtils.copyProperties(shopInfoVo, shopInfo);
        save(shopInfo);
        // 店铺绑定分配
        List<String> professions = shopInfoVo.getProfessions();
        if(! CollectionUtils.isEmpty(professions)){
            // 抛出异常
//            throw new ServiceException("");
            ArrayList<ShopProfession> shopProfessions = new ArrayList<>(professions.size());
            for (String  profession:  professions) {
                ShopProfession shopProfession = new ShopProfession();
                shopProfession.setProfessionId(profession);
                shopProfession.setShopId(shopInfo.getId());
                shopProfessions.add(shopProfession);
            }
            shopProfessionService.saveBatch(shopProfessions);
        }
        // 绑定用
        ShopUser shopUser = new ShopUser();
        shopUser.setShopId(shopInfo.getId());
        shopUser.setUserId(shopInfoVo.getUserId());
        shopUser.setIsDefault(Constants.YES);
        shopUser.setIsManager(Constants.YES);
        shopUserService.save(shopUser);
//        SysUserShop sysUserShop = new SysUserShop();
//        sysUserShop.setShopId(shopInfo.getId());
//        sysUserShop.setUserId(shopInfoVo.getUserId());
//        sysUserShopMapper.insert(sysUserShop);
        // 设置默认店铺
        SysUser sysUser = new SysUser();
        sysUser.setId(shopInfoVo.getUserId());
        sysUser.setShopId(shopInfo.getId());
        sysUserService.updateById(sysUser);
        return true;
    }

    @Override
    public Boolean updateShopInfo(ShopInfoVo shopInfoVo) {
        BeanValidators.isNotEmpty(shopInfoVo.getShopName());
        BeanValidators.isNotEmpty(shopInfoVo.getId());
        shopInfoVo.setDelFlag(null);
        shopInfoVo.setCreateBy(null);
        shopInfoVo.setCreateTime(null);
        updateById(shopInfoVo);
        List<String> professions = shopInfoVo.getProfessions();
        // 先删除旧的
        shopProfessionService.remove(new LambdaQueryWrapper<ShopProfession>().eq(ShopProfession::getShopId, shopInfoVo.getId()));
        if(! CollectionUtils.isEmpty(professions)){
            ArrayList<ShopProfession> shopProfessions = new ArrayList<>(professions.size());
            for (String  profession:  professions) {
                ShopProfession shopProfession = new ShopProfession();
                shopProfession.setProfessionId(profession);
                shopProfession.setShopId(shopInfoVo.getId());
                shopProfessions.add(shopProfession);
            }
            shopProfessionService.saveBatch(shopProfessions);
        }
        return true;
    }

    /**
     * 获取分类信息
     * @param userId
     * @return
     */
    @Override
    public ShopInfoVo getShopInfoByUserIdAndShopId(String userId, String shopId) {
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(shopId)){
            return null;
        }
        ShopInfoVo shopInfoVo = baseMapper.getShopInfoByUserIdAndShopId(userId, shopId);
        if(shopInfoVo == null){
            return null;
        }
        List<ShopProfessionVo> shopProfessionVos = shopProfessionService.selectShopProfessionByShopId(shopId);
        shopInfoVo.setProfessionList(shopProfessionVos);
        return shopInfoVo;
    }

    /**
     * 本月统计
     *
     * @param shopInfoVo
     * @return
     */
    @Override
    public ShopInfoVo currentMonthStatistics(ShopInfoVo shopInfoVo) {
        OrderInfoVo infoVo = orderInfoService.currentMonthStatistics(shopInfoVo.getId());
        shopInfoVo.setOrderInfoVo(infoVo);
        RestockInfoVo restockInfoVo = restockInfoService.currentMonthStatistics(shopInfoVo.getId());
        shopInfoVo.setRestockInfoVo(restockInfoVo);
        List<DebtRecordsVo> debtRecordsVos = debtRecordsService.currentMonthStatistics(shopInfoVo.getId());
        shopInfoVo.setDebtRecordsVos(debtRecordsVos);
        return shopInfoVo;
    }
}
